Debug Tree
This library allows you to build a tree one element at a time and output it as a pretty string.
The tree can easily be output to a String
, stdout
or a file.
This is particularly convenient for generating clean output from nested and recursive functions.
Recursive Fibonacci Example
Using the add_branch!()
macro at the start of the factors()
function, you can generate an entire call tree, with minimal effort.
use *;
A Fibonacci Tree
├╼ 6
│ ├╼ 1
│ ├╼ 2
│ │ └╼ 1
│ └╼ 3
│ └╼ 1
└╼ That's All Folks!
Overview
-
Add a branch
add_branch!("Hello, {}", "World")
- The branch will exit at the end of the current block
-
Add a leaf
add_leaf!("I am a {}", "leaf")
- Added to the current scoped branch
-
Print a tree, or write it to file at the end of a block
defer_print!()
defer_write!("filename.txt")
- The tree will be empty after these calls
- To prevent clearing, use
defer_peek_print!
anddefer_peek_write!
-
Handle multiple trees using named trees
add_branch_to!("A", "I'm a branch on tree 'A'")
add_leaf_to!("A", "I'm a leaf on tree 'A'")
defer_print!("A")
defer_write!("A", "filename.txt")
-
Get a named tree
tree("TREE_NAME")
-
Retrieve the pretty-string from a tree
tree("TREE_NAME").string()
-
Usage across threads
default_tree()
is local to each thread- Named trees are shared between threads
More Examples
Multiple Tagged Trees
If you need multiple, separated trees you can use a name tag.
use *;
A TREE
├╼ A TREE
│ └╼ A TREE
└╼ A TREE
└╼ A TREE
B TREE
├──> B TREE
│ ╰──> B TREE
├──> B TREE
│ ╰──> B TREE
╰──> B TREE
╰──> B TREE
Nested Functions
Branches also make nested function calls a lot easier to follow.
use *;
a
├╼ b
│ └╼ c
│ └╼ Nothing to see here
└╼ c
└╼ Nothing to see here
Line Breaks
Newlines in multi-line strings are automatically indented.
use *;
1
├╼ 1.1
│ Another line...
│ ... and one more line
└╼ 1.2
Panics
Even if there is a panic, the tree is not lost!
The defer_
functions were introduced to allow the tree
to be printed our written to file in the case of a panic!
or early return.
use *;
By using the 'defer_' functions
└╼ Output will still be generated
└╼ Otherwise you might lose your valuable tree!
Now for something crazy...
└╼ Here are my last words
└╼ Stay calm, and try not to panic
Without Macros
If you prefer not using macros, you can construct TreeBuilder
s manually.
use TreeBuilder;
1 Branch
└╼ 1.1 Child
2 Sibling